#include "mainwindow.h"
#include <QPainter>
#include <QMouseEvent>

int Select(int x1, int y1, int x2, int y2, int x, int y)  // Алгоритм Кохена-Сазерленда
{
    int i = 0;
    if ((y2 - y) < 0) i = 8;  // точка выше окна
    if ((y - y1) < 0) i = 4;  // точка ниже окна
    if ((x2 - x) < 0) i += 2; // точка правее окна
    if ((x - x1) < 0) i += 1; // точка левее окна
    return i;
}

OpenGLWindow::OpenGLWindow(QWidget* parent)
        :QGLWidget(parent)
/*
     Если child, то Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint
     по умолчанию, а setAttribute(Qt::WA_TranslucentBackground) (прозрачный фон окна)
     игнорируется.
*/
//    :QGLWidget(0, 0, Qt::FramelessWindowHint/*|Qt::WindowStaysOnTopHint*/)
{
    QGLFormat fmt = format();
    setFormat(fmt);
}
void OpenGLWindow::initializeGL( )
{
    initializeOpenGLFunctions();
    glClearColor(0, 0, 0, 0);
//    glShadeModel(GL_FLAT); // Режимом сглаживания цветов (GL_SMOOTH)
//    glEnable(GL_NORMALIZE);
//    glEnable(GL_LIGHTING);
}
void OpenGLWindow::resizeGL (int nWidth, int nHeight)
{
    glViewport(0, 0, nWidth, nHeight);
}
void OpenGLWindow::paintGL ()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//    glEnable (GL_BLEND);
//    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    float i = 1;
    glBegin(GL_QUADS);
       glColor4f(1, 0, 0, 0.2);
       glVertex2f(-i, i);
       glColor4f(0, 1, 0, 0.2);
       glVertex2f(i, i);
       glColor4f(0, 0, 1, 0.2);
       glVertex2f(i, -i);
       glColor4f(1, 1, 1, 0.2);
       glVertex2f(-i, -i);
    glEnd();
}



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    bwhs = false;
    pgl = nullptr;
    setMouseTracking(true); // Enable mouseMoveEvent with no button pressed
}

MainWindow::~MainWindow()
{
    if(pgl) delete pgl;
}
void MainWindow::button_Next() // Кнопка  >>
{
    qDebug() << "button_Next ";

}

bool bwds = false;
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    if(bwhs) {
        whs.width = this->width();
        whs.height = this->height();
        whs.showWhs(&p);
    }
    else {
        wds.Show(&p);
        wds.showButtons(this);
    }
    if(pgl == nullptr) {
        pgl = new OpenGLWindow(this);
//        pgl->setAttribute(Qt::WA_TranslucentBackground);
        pgl->move(200,340);
        pgl->resize(200,200);
        pgl->show();
    }
}
void MainWindow::mousePressEvent(QMouseEvent* mouse)
{
    if(mouse->buttons() & Qt::RightButton) {
        if(bwhs) {
            if(whs.gensh.iView != 0) {
                whs.gensh.iView = 0;
                update();
                return;
            }
            else {
                openWds();
                update();
                return;
            }
        }
        else {
            int i = wds.checkWhs(mouse->pos());
            if(i != -1) {
                CWhs* pwhs = wds.whs.at(i);
                if(pwhs->gensh.iView != 0) {
                    pwhs->gensh.iView = 0;
                    update();
                    return;
                }
                else {
                    openWds();
                    update();
                    return;
                }
            }
        }
    }
    if(mouse->buttons() & Qt::LeftButton) {
        if(bwhs) {
            whs.gensh.iView++;
            if(whs.gensh.iView >= whs.gensh.iViews) whs.gensh.iView = 0;
            update();
        }
        else {
            int i = wds.checkWhs(mouse->pos());
            if(i != -1) {
                CWhs* pwhs = wds.whs.at(i);
                pwhs->gensh.iView++;
                if(pwhs->gensh.iView >= pwhs->gensh.iViews) pwhs->gensh.iView = 0;
                update();
            }
        }
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent* mouse)
{
    if(bwhs) {
        int i = whs.checkWhs(mouse->pos());
        if(i == 0) {
            if(whs.hlight != 0) { whs.hlight = 0; update(); }
        }
        else {
            if(whs.hlight != i) { whs.hlight = i; update(); }
        }
    }
    else{
        int i = wds.checkWhs(mouse->pos());
        if(i == -1) return;
        CWhs* pwhs = wds.whs.at(i);
        i = pwhs->checkWhs(mouse->pos());
        if(i == 0) {
            if(pwhs->hlight != 0) { pwhs->hlight = 0; update(); }
        }
        else {
            if(pwhs->hlight != i) { pwhs->hlight = i; update(); }
        }
    }
}
